// ==UserScript==
// @name         Wod显示物品详情
// @namespace    https://github.com/knight000/Wod_Script
// @version      2022.12.19.1
// @description  try to take over the world!
// @author       knight000
// @match        http*://*.world-of-dungeons.org/wod/spiel/hero/items.php*
// @match        http*://*.world-of-dungeons.org/wod/spiel/trade/trade.php*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=world-of-dungeons.org
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// @grant        unsafeWindow
// @modifier     knight000
// @downloadURL  https://gitee.com/christophero/wod-scripts/raw/master/wod/standard/WoDItemDetailTip.user.js
// @updateURL    https://gitee.com/christophero/wod-scripts/raw/master/wod/standard/WoDItemDetailTip.user.js
// ==/UserScript==

let detailsDivNotHold;
let oldItemLink;
(function () {
  "use strict";
  // 页面加载完毕后执行脚本
  window.onload = function () {
    let $itemList = $(
      "a.item_unique,a.item_usable,a.item_unusable,a.item_later_usable"
    );
    // 给物品链接添加属性
    for (let i = 0; i < $itemList.length; i++) {
      let $item = $itemList.eq(i);
      $item.attr(
        "onmouseover",
        'window.ItemDetails_Show("' + $item.attr("href") + '")'
      );
      $item.attr("onmouseout", "ItemDetails_Hide()");
    }
    // 添加显示的Div
    let $nd = $("<div></div>");
    $nd.attr({
      id: "ItemDiv",
      onmouseover: "ItemDetails_Hold()",
      onmouseout: "ItemDetails_Hide()",
      overflow: "auto",
    });
    $nd.css({
      padding: "15px",
      weight: "auto",
      height: "auto",
      position: "absolute",
      "z-index": "999",
      border: "1px none",
      background: "#000000",
      "border-radius": "5px",
      background: "linear-gradient(to bottom right,#292929,#222222)",
      padding: "10px 7px 3px",
      border: "solid 1px #bda45b",
      "box-shadow": "rgb(0 0 0 / 70%) 0px 1px 8px",
      "font-size": "14px",
      color: "#ccc",
      "max-height": "450px",
      overflow: "auto",
    });
    $nd.hide();
    $("body").append($nd);
    detailsDivNotHold = true;
    // 默认关闭ajax请求的异步处理，将flase改成true就打开，关闭异步可以防止多个请求叠在一起，但是在网络卡的时候会导致页面崩溃
    $.ajaxSetup({
      async: true,
    });
  };

  /**
   * @function 显示
   */
  unsafeWindow.ItemDetails_Show = function ItemDetails_Show(ItemLink) {
    let $itemDiv = $("#ItemDiv");
    // 判断是否持续
    if (detailsDivNotHold == true) {
      detailsDivNotHold = false;
      if (oldItemLink != ItemLink) {
        oldItemLink = ItemLink;
        $itemDiv.html("");
        // 获取物品内容
        $.get(ItemLink, function (result) {
          if (!result.includes("<html>")) {
            result = "<html>" + result;
          }
          // result = "<div>" + result + "</div>";
          const $doc = $(result);
          let $itemTitle = $doc.find("h1");
          $itemDiv.append($itemTitle.clone());
          let $base = $itemTitle;
          while (
            ($base = $base.next()) &&
            $base.length &&
            $base.text() != "到"
          ) {
            $itemDiv.append($base.clone());
          }
          let $itemDetils = $("#details", $doc);
          let $itemMore = $("#link", $doc);
          $('input[name="back"]', $doc).remove();
          let $back = $('<button class="button clickable">关闭</button>')
            .css({
              position: "sticky",
              bottom: "5px",
              left: "100%",
            })
            .click(() => ItemDetails_Hide(true));

          $("#ItemDiv").append($itemDetils, $itemMore, $back);
        });
        // 获取鼠标相对于文档的坐标并将div设置到那
        let position = getMousePosition(event);
        $itemDiv.css("left", position.x + 20);
        $itemDiv.css("top", position.y - 100);
      }
      $itemDiv.show();
    }
  };

  /**
   * @function 隐藏
   */
  unsafeWindow.ItemDetails_Hide = function ItemDetails_Hide(immediate) {
    detailsDivNotHold = true;
    setTimeout(
      function DivHide() {
        if (detailsDivNotHold == true) {
          $("#ItemDiv").hide();
        }
      },
      immediate ? 50 : 300
    );
  };

  /**
   * @function 保持
   */
  unsafeWindow.ItemDetails_Hold = function ItemDetails_Hold() {
    detailsDivNotHold = false;
  };
})();

/**
 * @function 获取鼠标相对于文档的坐标（考虑页面滚动）
 * @description 来源https://www.hangge.com/blog/cache/detail_2261.html
 * */
function getMousePosition(event) {
  let x = 0;
  let y = 0;
  let doc = document.documentElement;
  let body = document.body;
  if (!event) event = window.event;
  if (window.pageYoffset) {
    //pageYoffset是Netscape特有
    x = window.pageXOffset;
    y = window.pageYOffset;
  } else {
    x =
      ((doc && doc.scrollLeft) || (body && body.scrollLeft) || 0) -
      ((doc && doc.clientLeft) || (body && body.clientLeft) || 0);
    y =
      ((doc && doc.scrollTop) || (body && body.scrollTop) || 0) -
      ((doc && doc.clientTop) || (body && body.clientTop) || 0);
  }
  x += event.clientX;
  y += event.clientY;
  return { x: x, y: y };
}
